#!/bin/bash

current_time=$(date +%s)

for container_id in $(docker ps -a -q); do
    echo "container_id: ${container_id}"
    container_name=$(docker inspect --format '{{.Name}}' $container_id | sed 's/\///g')
    echo "container_name: ${container_name}"
    if [[ $container_name == rsv* ]]; then
        continue
    fi
    container_start_time=$(docker inspect --format '{{.State.StartedAt}}' $container_id)
    if [[ $(docker inspect --format '{{.State.Status}}' $container_id) == "exited" ]]; then
        container_start_time=$(docker inspect --format '{{.State.FinishedAt}}' $container_id)
    fi
    echo "container_start_time: ${container_start_time}"
    container_start_timestamp=$(date -d $container_start_time +%s)
    running_time=$((current_time - container_start_timestamp))
    echo "running_time: ${running_time}"
    if (( running_time > 600 )); then
        echo "Killing container: $container_name ($container_id)"
        docker stop $container_id
        docker rm $container_id
    fi
done


